// 防抖方法
import debounce from './debounce.js'
// 节流方法
import throttle from './throttle.js'
const utils = {
	debounce,
	throttle,
	/**
	 * 设置缓存
	 * @param {String} key 键
	 * @param {Object} value 值
	 * @param {Number} time 过期时间
	 */
	setStorageSyncTime(key, value, time) {
		uni.setStorageSync(key, value)
		let t = parseInt(time);
		if (t > 0) {
			let timestamp = Date.parse(new Date());
			timestamp = timestamp / 1000 + t;
			uni.setStorageSync(key + '_expire', timestamp + "")
		} else {
			uni.removeStorageSync(key + '_expire')
		}
	},
	/**
	 * 读取缓存
	 * @param {String} key 键
	 * @return {Object} value 值
	 */
	getStorageSyncTime(key) {
		let expire = parseInt(uni.getStorageSync(key + '_expire'))
		if (expire) {
			if (parseInt(expire) < Date.parse(new Date()) / 1000) {
				uni.removeStorageSync(key);
				uni.removeStorageSync(key + '_expire');
				return '';
			}
		}
		let res = uni.getStorageSync(key);
		if (res) {
			return res;
		} else {
			return '';
		}
	},
	/**
	 * 清除缓存
	 * @param {String} key 键
	 */
	removeStorageSyncTime(key) {
		uni.removeStorageSync(key)
		uni.removeStorageSync(key + '_expire')
	},
	
	getToken() {
		return this.getStorageSyncTime('token', 518400) || ''
	},
	
	setToken(token) {
		this.setStorageSyncTime('token', token)
	},
	
	toast({title, icon, duration}, callback = null) {
		let obj = {
			title: title || '操作成功',
			icon: icon || 'none',
			duration: duration || 1000
		}
		uni.showToast(obj)
		if (callback) {
			let timer = setTimeout(() => {
				clearTimeout(timer)
				callback && callback()
			}, obj.duration)
		}
	},
	
	msgBox(obj) {
		uni.showModal({
			title: '提示',
			...obj
		})
	},
	
	goUrl(url, fun = 'navigateTo') {
		uni[fun]({
			url
		})
	},
	
    datastime(type){
		let myDate = new Date();  //获取当前时间
		let yinian=myDate.getFullYear(); //年
		let yer=parseInt(myDate.getMonth())+1;//月份
		if(yer<=9){yer="0"+yer;}
		let riqi=myDate.getDate();//日
		if(riqi<=9){riqi="0"+riqi;}
		let time=myDate.getHours();//获取当前小时数(0-23)
		if(time<=9){time="0"+time;}
		let fen=myDate.getMinutes();     //获取当前分钟数(0-59)
		if(fen<=9){fen="0"+fen;}
		let miao=myDate.getSeconds();     //获取秒钟数
		if(miao<=9){miao="0"+miao;}
		if(type=='nyr'){return yinian+"年"+yer+"月"+riqi+"日"} // 2021年1月14日
		if(type=='yous'){
			return yinian+"-"+yer+"-"+riqi+" "+time+":"+fen+":"+miao //2021-01-14 16:47:36
		}
		if(type=='int'){return parseInt(yinian+""+yer+""+riqi+time+""+fen)}//202101141647
		return yinian+"年"+yer+"月"+riqi+"日"+" "+time+":"+fen+":"+miao //2021年1月14日 16:47:36
	},
	
	nformatTime(number, format) {
	  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	  var returnArr = [];
	  var date = new Date(number * 1000);
	  returnArr.push(date.getFullYear());
	  returnArr.push(this.formatNumber(date.getMonth() + 1));
	  returnArr.push(this.formatNumber(date.getDate()));
	  returnArr.push(this.formatNumber(date.getHours()));
	  returnArr.push(this.formatNumber(date.getMinutes()));
	  returnArr.push(this.formatNumber(date.getSeconds()));
	  for (var i in returnArr) {
	    format = format.replace(formateArr[i], returnArr[i]);
	  }
	  return format;
	},
	
	formatNumber(num){
		if(num<9){
			return "0"+num
		}else{
			num
		}
	}
}

export default utils
